package lvxu.service;

import lvxu.bean.Music;
import lvxu.bean.User;
import lvxu.bean.UserExample;
import lvxu.dao.MusicMapper;
import lvxu.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;


    public boolean login(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(user.getUsername());
        criteria.andUserpasswordEqualTo(user.getUserpassword());
        long index =  userMapper.countByExample(userExample);
        if (index != 0){
            return true;
        }else {
            return false;
        }
    }

    public User selectByUserId(Integer userid) {
        User user = userMapper.selectByPrimaryKey(userid);
        return user;
    }

    public void updateState(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    public User updateUser(User user) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUseridEqualTo(user.getUserid());
        User user1 = userMapper.selectByPrimaryKey(user.getUserid());
        if(user.getUsername() == null){
            user.setUsername(user1.getUsername());
        }
        if (user.getUserpassword() == null){
            user.setUserpassword(user1.getUserpassword());
        }
        if (user.getGender() == null){
            user.setGender(user1.getGender());
        }
        if (user.getPhone().isEmpty()){
            user.setPhone(user1.getPhone());
        }
        if (user.getEmail().isEmpty()){
            user.setEmail(user1.getEmail());
        }
        if (user.getUserstate() == null){
            user.setUserstate(user1.getUserstate());
        }
        userMapper.updateByExample(user, userExample);
        User user2 = userMapper.selectByPrimaryKey(user.getUserid());
        System.out.println("=====================================");
        System.out.println(user);
        System.out.println("=====================================");

        return user2;
    }

    public List<User> selectUser(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);
        return users;
    }

    public boolean saveUser(User user) {
        int insert = userMapper.insert(user);
        return insert!=0?true:false;
    }

    public List<User> selectUsers() {
        List<User> users = userMapper.selectByExample(null);
        return users;
    }

    public boolean deleteById(Integer id) {
        int delete = userMapper.deleteByPrimaryKey(id);
        return delete !=0 ? true:false;
    }

}
